<template lang="html">
  <div class="icons">
    <swiper :options="swiperOption" v-if="showIcon">
      <swiper-slide v-for="(page, index) of pages" :key="index">
        <div class="icon" v-for="(item) of page" :key="item.id">
          <div class="icon-box">
            <img class="icon-img" :src="item.imgUrl" alt="">
            <p class="icon-desc">{{item.desc}}</p>
          </div>
        </div>
      </swiper-slide>
    </swiper>
  </div>
</template>

<script>
export default {
  name: 'HomeIcons',
  props: {
    iconList: Array
  },
  data () {
    return {
      swiperOption: {
        loop: true,
        autoplay: false
      }
    }
  },
  computed: {
    showIcon () {
      return this.iconList.length
    },
    pages () {
      const pages = []
      this.iconList.forEach(function (ele, index, arr) {
        const page = Math.floor(index / 8)
        if (!pages[page]) {
          pages[page] = []
        }
        pages[page].push(ele)
      })
      return pages
    }
  }
}
</script>

<style lang="stylus" scoped>
  @import '~styles/variable.styl'
  @import '~styles/mixins.styl'
  .icons >>> .swiper-container
    height: 0
    padding-bottom: 50%
  .icons
    overflow: hidden
    width: 100%
    height: 0
    padding-bottom: 50%
    margin-top: .1rem
    .icon
      float: left
      position: relative
      overflow: hidden
      width: 25%
      height: 0
      padding-bottom: 25%
      .icon-box
        position: absolute
        top: 0
        right: 0
        bottom: 0
        left: 0
        .icon-img
          display : block
          box-sizing: border-box
          padding: .1rem .1rem .44rem .1rem
          height: 100%
          margin: 0 auto
        .icon-desc
          position: absolute
          bottom: 0
          width: 100%
          box-sizing: border-box
          text-align : center
          padding: 0 .1rem
          ellipsis()
</style>
